package com.lebang.service.cences;

import com.lebang.domain.CencesMaster;
import com.lebang.example.CencesMasterExample;
import com.lebang.mapper.CencesApruMapper;
import com.lebang.mapper.CencesMasterMapper;
import com.lebang.mapper.LbbMemberMapper;
import com.lebang.model.MyConfig;
import com.lebang.model.cences.CencesMsg;
import com.lebang.model.cences.MasterCountModel;
import com.lebang.model.cences.MasterMoneyModel;
import com.lebang.service.BaseService;
import com.lebang.service.mongodb.CencesMsgMongodb;
import com.lebangweb.common.TextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 师徒统计
 *
 * @Author hwq
 * @Date 2020-02-04
 */
@Service
@Slf4j
public class CencesMasterService extends BaseService<CencesMaster, Integer, CencesMasterExample> {
    @Autowired
    private CencesMasterMapper cencesMasterMapper;
    @Autowired
    private CencesApruMapper cencesApruMapper;
    @Autowired
    private LbbMemberMapper lbbMemberMapper;
    @Autowired
    private CencesMsgMongodb cencesMsgMongodb;

    public void starDataInit(LocalDateTime end) {
        this.starDataInit(end,null);
    }
    /**
     * 开始统计师徒数据
     */
    public void starDataInit(LocalDateTime end, List<String> brand) {
        long starTime=System.currentTimeMillis();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDateTime star = end.minusDays(1);
        LocalDate starDate = star.toLocalDate();
        int time = Integer.parseInt(end.toLocalDate().minusDays(1).format(df));
        List<CencesMaster> cencesMasters = new ArrayList<>();
        if(brand==null){
            brand=lbbMemberMapper.selectBrand();
        }
        //收徒数
        List<MasterCountModel> masterCountModels = cencesMasterMapper.selectByTime( star, end);
        Map<String ,List<MasterCountModel>> listMap=masterCountModels.stream().collect(Collectors.groupingBy(MasterCountModel::getAppStore));
        //徒弟支出
         List<MasterMoneyModel> masterMoneyModels=cencesMasterMapper.selectRebateApprenticeMoney(null, star, end, 0);
        Map<String ,List<MasterMoneyModel>> listMap1=masterMoneyModels.stream().collect(Collectors.groupingBy(MasterMoneyModel::getAppStore));
        //收徒孙数
        List<MasterCountModel> stts = cencesMasterMapper.selectAllTsMember(null, star, end);
        Map<String ,List<MasterCountModel>> sttsMap=stts.stream().collect(Collectors.groupingBy(MasterCountModel::getAppStore));
        //徒孙支出
        List<MasterMoneyModel> tszc=cencesMasterMapper.selectRebateApprenticeMoney(null, star, end, 1);
        Map<String ,List<MasterMoneyModel>> tszcmap=tszc.stream().collect(Collectors.groupingBy(MasterMoneyModel::getAppStore));
        //全部用户的
        brand.forEach(s -> {
            CencesMaster newVo = new CencesMaster(0, s, time, 1, 0, MyConfig.getEnv());
            CencesMaster oldVo = new CencesMaster(0, s, time, 0, 0,MyConfig.getEnv());
            CencesMaster allVo = new CencesMaster(0, s, time, 2, 0,MyConfig.getEnv());
            //收徒数
            List<MasterCountModel> countModels = this.review(listMap,s);
            countModels.forEach(countModel -> {
                if (countModel.getCreateTime().equals(starDate)) {
                    //今天新增的用户
                    newVo.setApprenticeCount(newVo.getApprenticeCount() + 1);
                } else {
                    //老用户
                    oldVo.setApprenticeCount(oldVo.getApprenticeCount() + 1);
                }
                allVo.setApprenticeCount(allVo.getApprenticeCount() + 1);
            });
            //徒弟支出
            List<MasterMoneyModel> masterModels = this.reviewMasterMoney(listMap1,s);
                    //cencesMasterMapper.selectRebateApprenticeMoney(s, star, end, 0);
            masterModels.forEach(vo -> {
                if (vo.getCreateTime().equals(starDate)) {
                    //今天新增的用户
                    newVo.setRebateApprenticeMoney(newVo.getRebateApprenticeMoney() + vo.getMoney());
                } else {
                    //老用户
                    oldVo.setRebateApprenticeMoney(oldVo.getRebateApprenticeMoney() + vo.getMoney());
                }
                allVo.setRebateApprenticeMoney(allVo.getRebateApprenticeMoney() + vo.getMoney());
            });
            //徒孙数
            List<MasterCountModel> tsCount = this.review(sttsMap,s);
            //cencesMasterMapper.selectAllTsMember(s, star, end);
            tsCount.forEach(countModel -> {
                if (TextUtil.isEmpty(countModel.getInviterId())) {
                    if (countModel.getCreateTime().equals(starDate)) {
                        //今天新增的用户
                        newVo.setDiscipleCount(newVo.getDiscipleCount() + 1);
                    } else {
                        //老用户
                        oldVo.setDiscipleCount(oldVo.getDiscipleCount() + 1);
                    }
                    allVo.setDiscipleCount(allVo.getDiscipleCount() + 1);
                }
            });
            //徒孙支出
            List<MasterMoneyModel> tsoUT = this.reviewMasterMoney(tszcmap,s);
            tsoUT.forEach(vo -> {
                if (vo.getCreateTime().equals(starDate)) {
                    //今天新增的用户
                    newVo.setRebateDiscipleMoney(newVo.getRebateDiscipleMoney() + vo.getMoney());
                } else {
                    //老用户
                    oldVo.setRebateDiscipleMoney(oldVo.getRebateDiscipleMoney() + vo.getMoney());
                }
                allVo.setRebateDiscipleMoney(allVo.getRebateDiscipleMoney() + vo.getMoney());
            });
            cencesMasters.add(newVo);
            cencesMasters.add(oldVo);
            cencesMasterMapper.insertAll(allVo);
            cencesMasterMapper.insertAll(newVo);
            cencesMasterMapper.insertAll(oldVo);
        });
        //全部
        CencesMaster item = new CencesMaster(0, "all", time, 2,1,MyConfig.getEnv());
        //新用户全部
        CencesMaster newAll = new CencesMaster(0, "all", time, 1, 1,MyConfig.getEnv());
        //老用户全部
        CencesMaster oldAll = new CencesMaster(0, "all", time, 0, 1,MyConfig.getEnv());

        cencesMasters.forEach(vo -> {
           sumBy(item,vo);
            if(vo.getCategory().equals(1)){
                sumBy(newAll, vo);
            }
            if(vo.getCategory().equals(0)){
                sumBy(oldAll, vo);
            }
        });
        cencesMasterMapper.insertAll(item);
        cencesMasterMapper.insertAll(newAll);
        cencesMasterMapper.insertAll(oldAll);
        //渠道的全部
        brand.forEach(s -> {
            CencesMaster sjy = new CencesMaster(0, s, time, 2, 1,MyConfig.getEnv());
            cencesMasters.forEach(vo -> {
                if(vo.getBrand().equals(s)){
                    sumBy(sjy, vo);
                }
            });
            cencesMasterMapper.insertAll(sjy);
        });
        cencesMsgMongodb.save(new CencesMsg("师徒统计",System.currentTimeMillis()-starTime));
    }


    private List<MasterMoneyModel> reviewMasterMoney(Map<String, List<MasterMoneyModel>> listMap, String appStore) {
        List<MasterMoneyModel> models=listMap.get(appStore);
        if(models==null){
            models=new ArrayList<>();
        }
        return models;
    }

    private List<MasterCountModel> review(Map<String, List<MasterCountModel>> listMap, String appStore) {
        List<MasterCountModel> models=listMap.get(appStore);
        if(models==null){
            models=new ArrayList<>();
        }
        return models;

    }

    public CencesMaster sumData(Integer star, Integer end, String appStore, int category, int type, String likeBrand) {
        return cencesMasterMapper.sumData(star, end, appStore, category, type,likeBrand,MyConfig.getEnv());
    }
    private void sumBy(CencesMaster item, CencesMaster vo) {
        item.setApprenticeCount(item.getApprenticeCount() + vo.getApprenticeCount());
        item.setRebateApprenticeMoney(item.getRebateApprenticeMoney() + vo.getRebateApprenticeMoney());
        item.setDiscipleCount(item.getDiscipleCount() + vo.getDiscipleCount());
        item.setRebateDiscipleMoney(item.getRebateDiscipleMoney() + vo.getRebateDiscipleMoney());
    }

    public int deleteByTime(int time) {
        CencesMasterExample earnDayExample = new CencesMasterExample();
        earnDayExample.createCriteria().andTimeEqualTo(time).andAppIdEqualTo(MyConfig.getEnv());
        return cencesMasterMapper.deleteByExample(earnDayExample);
    }
}
